Unity3D Building Damage Tutorial- Part III - Simple Damage
So, lets add a simple damage, really easy thing to just test what we have. For Example, lets make a function that will delete the given percent of height. If you will pass 0.01 parameter, the verticles in upper 1 % of mesh will dissappear. First, i changed the cube, to more complicated primitive, a capsule, like this on photo. Obviously, we have to add a damagemaking method, but before lets look at CalcualateMinimumHeight() function in MeshManager Class. CalculateMinimumHeight method As you may remember, it was called in the constructor: ... ManageVerticles(mesh.vertices); ManageTriangles(mesh.triangles); CalcualateMinimumHeight(); } Before we will look at code, I have to make something clear. Despite its name, it calculates both minimum AND maximum height. Its obvious when you see it. public void CalcualateMinimumHeight(){ Vector3[] verticles = mesh.vertices; foreach(Verticle vert in Aliases){ if(vert.positionAbsolute.y < MinimumHeight){ MinimumHeight = vert.positionAbsolute.y;} if(vert.positionAbsolute.y > MaximumHeight){ MaximumHeight = vert.positionAbsolute.y;} } } Well, its simple. First a foreach loop, than asking every Alias about its absolute height (positionAbsolute.y). If it is higher then our current MaximumHeight, MaximumHeight is set to this height. Similarly it is done with Minimum Height. Destroy method public void Destroy (float percent){ float DestroyHeight = MaximumHeight -(MaximumHeight - MinimumHeight)*percent; foreach(Verticle vert in Aliases){ vert.DestroyByHeight(DestroyHeight); } UpdateTrianglesList(); } First, we are calculating the DestroyHeight: when Alias is higher than it, it should be destroyed. Than, we start a foreach loop and call the DestroyByHeight method to every Alias. Now, it is time, to look at this function, in Verticle.cs DestroyByHeight method (Verticle.cs) public void DestroyByHeight(float height){ if(heightOwnerManager.Aliasesk.RemoveLink(number); *OwnerManager.Aliasesk Here we check which Alias has this verticle. *.RemoveLink(number); Than we send an info to this Alias, that verticle of this number should be removed from LinkedAliases list. Lets look at this function. public void RemoveLink(int k){ int index = LinkedAliases.IndexOf(k); LinkedAliases.Remove(index); } *RemoveLink(k); Oki, than we have to remove links from this Alias. In fact, as we teoretically delete this, this is not necessary, but i still insert this removal here. Maybe we will Re-Remove it later? *Here we make foreach loop in Triangles list. And sent info about this triangle being destroyed to the current focused LinkedAlias k foreach(int l in Triangles){ OwnerManager.Aliasesk.RemoveTriangle(l); } RemoveTriangle method(Verticle.cs) public void RemoveTriangle(int number){ if(Triangles.Contains(number)){ Triangles.Remove(number); } } We see if our Triangle List contains giver triangle number. If so, deletes it. Back to Destroy (Verticle.cs) Now, we stat last method here: DestroyTriangles. See, first we tell all other Aliases that our links and triangles will cease to exist. Now we will tell the MeshManager about it, and he will pass this info to Unity. DestroyTriangles method (Verticle.cs) private void DestroyTriangles(){ for(int i = 0 ; iVector3 triangleNumbers = Trianglesk; We get a Vector3 with Verticle numbers which make this triangles. *Vector3 aliasNumbers = ChangeVerticleToAlias(triangleNumbers); We translate it to AliasNumbers. *You see, this two above are not necessary, as i am not using it. But maybe i will use it later *Trianglesk = Vector3.zero; This is interesting. I set all numbers here to zero. As a result, there will be no triangle. Teoretically, I could remove this Vector3 compleatly, but it will affect overal triangle numbers. Back to Destroy method Now we start our last function. UpdateTrianglesList(); UpdateTrianglesList(); public void UpdateTrianglesList(){ int LenghtOfTrianglesArray = Triangles.Count*3; int[] tempTrianglesArray = new intLenghtOfTrianglesArray; int i = 0; foreach(Vector3 vec in Triangles){ tempTrianglesArrayi = (int)vec.x; tempTrianglesArrayi+1 = (int)vec.y; tempTrianglesArrayi+2 = (int)vec.z; i+=3; } mesh.triangles = tempTrianglesArray; } This function translates the Triangles list to mesh.triangles. What is intresting, the meshManager and Aliases should operate only on Triangles list.Than, this function should be called that will translate list and assign it to mesh.triangles. Starting Destroy function I am calling it in Building.cs, line 182, and i am doing it for each meshfilter. public void FireDamage(){ foreach(MeshManager manager in meshManagers){ manager.Destroy(0.02f); } All classes of building like WarFactory or Refinery are deriverd from Building class. The FireDamage method is called when you click on building. So, lets see the result. End Well, still there are some things to do. First, if we rotate the object, the same verticles will dissappear. Second, I belive the Alias still holds the data about its Triangles. Category:Unity3D Building Damage Tutorial Category:Unity3D Category:C# Category:C Sharp